<?php

namespace App\View;

class Base
{
    const TPL_SUFFIX = '.phtml';

    protected $_basePath = '';

    protected $_cachePath = 'cache/';

    public function out($var, $is = TRUE)
    {
        if (!is_scalar($var))
        {
            throw new RuntimeException('$var 必须是一个标量');
        }
        if ($is && !is_numeric($var))
        {
            echo htmlspecialchars($var, ENT_QUOTES);
        }
        else
        {
            echo $var;
        }
    }

    public function setBasePath($path)
    {
        $this->_basePath = $path;
    }

    public function getBasePath($mod_name = '')
    {
        if ($mod_name !== '') {
            $config = include PROJECT_PATH . "modules/{$mod_name}\config\{$mod_name}.php";
            return $config['tplBasePath'];
        }

        return $this->_basaPath;
    }

    public function findTpl($name)
    {
        if (strpos($name, '.') !== FALSE) {
            $file_parts = explode('.', $name);
            $tplfile  = $this->getBasePath(array_shift($file_parts));
            $tplfile .= explode(DS, $file_parts) . self::TPL_SUFFIX;
        } else {
            $tplfile = $this->_basePath . $name . self::TPL_SUFFIX;
        }

        return $tplfile;
    }

    public function findCacheFile($path, $md5)
    {
        return $path . $md5;
    }
}